package workCount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFS_client {
    Configuration conf;
    FileSystem fs;
    String URL = "hdfs://192.168.35.131:9000";
    String localDir = "D:\\data\\file\\wordCount.txt";
    //固定变量
    //定义HDFS用户
    String user = "root";
    //定义处理的原始数据导入路径
    String srcDir = "/input";
    //定义处理完成输出的路径
    String destDir = "/output";
    //需要处理的文件所在的HDFS位置
    String fileName = srcDir + "/wordCount.txt";
    public HDFS_client(String URL,String user) throws IOException, URISyntaxException, InterruptedException {
        conf = new Configuration();
        conf.set("fs.defaultFS",URL);
        conf.set("dfs.replication","1");
        fs = FileSystem.get(new URI(URL),conf,user);
//        System.setProperty("HADOOP_USER_NAME",user);
//        System.setProperty("HADOOP_ROOT+PASSWORD","1");
    }

//    public void init() throws IOException, URISyntaxException, InterruptedException {
//        conf = new Configuration();
//        conf.set("fs.defaultFS",URL);
//        fs = FileSystem.get(new URI(URL),conf,user);
//    }

    public void createDir(String srcDir) throws IOException {
        Path path = new Path(srcDir);
        if(fs.exists(path)){
            delFile(srcDir);
        }
        fs.mkdirs(path);
        System.out.println("create success");
    }


    public void uploadFile(String localDir,String srcDir) throws IOException {
        fs.copyFromLocalFile(new Path(localDir), new Path(srcDir));
        System.out.println("Put file success");
    }


    public void printFile(String fileName) throws IOException {

        RemoteIterator<LocatedFileStatus> status = fs.listFiles(new Path(fileName),true);
        InputStream in;
        LocatedFileStatus fileStatus;
        //定义一个Stream输入流,接受来自fs中读取到的文件内容
        while (status.hasNext()){
            fileStatus = status.next();
            in = fs.open(fileStatus.getPath());
            IOUtils.copyBytes(in,System.out,512);
            IOUtils.closeStream(in);
        }
    }

    //删除指定文件
    public void delFile(String pathName) throws IOException {
        Path path = new Path(pathName);
        fs.delete(path,true);
        System.out.println("\ndel success\n");
    }
    public void close() throws IOException {
        fs.close();
    }

}
